package com.atguigu.day16.array02;

import org.junit.Test;

import java.util.Arrays;

/*
操作数组的工具类: Arrays
   toString()
   sort()
   binarySearch()
   copyOf()
 */
public class ArraysTest {
    @Test
    public void test01() {
        int[] arr = {10, 20, 30};
        System.out.println("arr = " + arr);//[I@4ec6a292

        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));//[10, 20, 30]

        int[][] dArr = {{10, 20}, {30, 40}, {33, 44, 55, 66}};
        System.out.println("dArr = " + dArr);
        // System.out.println("Arrays.toString(dArr) = " + Arrays.toString(dArr));

        System.out.println("Arrays.deepToString(dArr) = " + Arrays.deepToString(dArr));//[[10, 20], [30, 40], [33, 44, 55, 66]]

    }

    @Test
    public void test02() {
        int[] arr = {30, 20, 10, 90, 70};

        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));
        //排序
        Arrays.sort(arr);
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));//[10, 20, 30, 70, 90]
        //二分查找
        int index = Arrays.binarySearch(arr, 700);
        System.out.println("index = " + index);


    }
    @Test
    public void test03(){
     int[]arr = new int[6];
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));
        //数据填充
        //Arrays.fill(arr,66);
        Arrays.fill(arr,1,4,33);
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));

    }
    @Test
    public void test04(){

        String[]arr = {"杨幂","赵丽颖","李冰冰"};

        //1.创建新数组
        String[]newArr = new String[arr.length<<1];
        //2.将旧数组元素添加到新数组内
        for (int i = 0; i < arr.length; i++) {
            newArr[i]=arr[i];
        }
        //3.将新数组地址赋值给旧数组
        arr = newArr;
        arr[3]="安琪拉";

        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));


    }
    @Test
    public void test05(){
        String[]arr = {"杨幂","赵丽颖","李冰冰"};
       // String[] newArr = Arrays.copyOf(arr, arr.length << 1);
        //todo 将旧数组元素放到新数组内并指定新数组的长度
        arr = Arrays.copyOf(arr, arr.length << 1);
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));

        arr[3]="安琪拉";
        System.out.println("Arrays.toString(arr) = " + Arrays.toString(arr));
    }

    @Test
    public void test06(){

        int[]arr = {10,20,30};
        int[]arr2 = {10,30,20};

        System.out.println(arr==arr2);
        //比较数组内容是否一样
        boolean equals = Arrays.equals(arr, arr2);
        System.out.println("equals = " + equals);

    }
}
